var pagNum1 = 0;
var userId = userid
jQuery(function () {
    initFavorite();
});
function initFavorite(){
    $.ajax({
        url:server_url + '/rest/favorite/getFavorites.do',
        data:{userId:userId,pageNum:pagNum1,rand:Math.random()},
        success:function(data){
            if(data.retCode != '0000')
            {
                easyAlert(data.info);
                return;
            }
            dealProductData(data.data);
        }
    })
}
var dealProductData = function (data) {
    var s = '';
    if (data != null && data.length > 0) {
        for (var i = 0; i < data.length; i++) {
            s += '\
            <div class="box_r" id="' + data[i].id + '" >\
            <div class="display_block">\
            <div class="det_box">\
            <img  onclick="setSku(' + data[i].id + ');" src="' + data[i].picturePath1 + '" alt="" class="">\
            <div class="">\
            <div class="con_tit">\
            <p>' + data[i].productName + '</p>\
            <span>¥' + data[i].price + '/头</span>\
            </div>\
            <p class="con_normal">类别：' + data[i].specName + '</p>\
            <p class="con_normal">库存：' + data[i].overPlusNum + '头</p>\
            <p class="con_normal">货源地：' + data[i].areaName + '</p>\
            <p class="con_normal">'+ data[i].addTime.substr(0,10) +'</p>\
            </div>\
            <div class="collect_box">\
            <p></p>\
            <span onclick="cancelFavorite(' + data[i].id + ');">取消收藏</span>\
            </div>\
            </div>\
            </div>\
            </div>';
        }
        $("#prodList").append(s);
        pagNum1++;
    } else {
        if (pagNum1 < 1) {
            s = "<li style='width:8.6rem; text-align:center; float:right; border-bottom:none;'>" +
            "<p style='font-size:0.3rem; padding:50% 5% 50% 5%;'>您暂时没有任何收藏 &nbsp;" +
            "</p>" +
            "</li>";
            $("#prodList").html(s);
        }

    }

}
var setSku = function(skuId){
    localStorage.setItem('skuId', skuId);
    window.location.href=server_url+"buyer/product/productDetail.do";
}

function cancelFavorite(skuId){
    var userid = userid
    $("#"+skuId).remove();
    $.ajax({
        url:server_url+'/rest/favorite/addToFavorite.do',
        data:{userId:userid,skuId:skuId,rand:Math.random()},
        success:function(){
            easyAlert("取消收藏成功");
        }
    });
}